
package controller;

import java.util.List;
import model.dao.ProductDao;
import model.domain.Product;
import model.domain.Sprint;
import org.primefaces.model.DualListModel;

public class plansprintBean {

    private Sprint sprint;
    private DualListModel<Product> picklist;

    public plansprintBean(){

    }

    public Sprint getSprint(){
        return this.sprint;
    }

    public void setSprint(Sprint object){
        this.sprint = object;
    }

    public DualListModel<Product> getPicklist() {

        List<Product> productBacklog;
        List<Product> sprintBacklog;

        ProductDao home = new ProductDao();
        productBacklog = home.findByProjectWithoutSprint(this.sprint.getProject());
        sprintBacklog = home.findBySprint(sprint);

        this.picklist = new DualListModel<Product>(productBacklog, sprintBacklog);

        return this.picklist;
    }
    public void setPicklist(DualListModel<Product> pickList) {
        this.picklist = pickList;
    }

    public void submitSave(){

        ProductDao home = new ProductDao();

        for (Product productTarget : this.picklist.getTarget()){
            productTarget.setSprint(this.sprint);
            productTarget.setProject(this.sprint.getProject());
            home.insertOrUpdate(productTarget);
        }

        for (Product productSource : this.picklist.getSource()){
            productSource.setSprint(null);
            productSource.setProject(this.sprint.getProject());
            home.insertOrUpdate(productSource);
        }
    }
}
